/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：NextJumpFinder.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router.biz;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.cneport.transfer.router.model.NodeRelation;

/**
 * @Descption :计算下一跳节点，计算思想是求生成树的最短路径
 * @CreateTime:2012-7-11
 */
@Component
public class NextJumpFinder {

    public String getNextJump(List<NodeRelation> map, String sourcePort,
            String destPort) {
        int a = -1;
        String nextPort = null;
        for (NodeRelation item : map) {
            List<String> all = new ArrayList<String>();
            all.add(item.getScrNodeId());
            if (sourcePort.equals(item.getScrNodeId())) {
                if (item.getDestNodeId().equals(destPort)) {
                    int b = item.getWeight();
                    if (b < a || a == -1) {
                        a = b;
                        nextPort = item.getDestNodeId();
                    }
                } else {
                    int b = getLeastCost(map, all, item.getDestNodeId(),
                            destPort);
                    int c = b + item.getWeight();
                    if (b != -1) {
                        if (a == -1) {
                            a = c;
                            nextPort = item.getDestNodeId();
                        } else {
                            if (c < a) {
                                a = c;
                                nextPort = item.getDestNodeId();
                            }
                        }
                    }
                }
            }
        }
        return nextPort;

    }

    private int getLeastCost(List<NodeRelation> map, List<String> all,
            String sourcePort, String destPort) {
        int a = -1;
        if (!all.contains(sourcePort)) {
            all.add(sourcePort);
            for (NodeRelation item : map) {
                if (sourcePort.equals(item.getScrNodeId())) {
                    if (!all.contains(item.getDestNodeId())) {
                        if (item.getDestNodeId().equals(destPort)) {
                            int b = item.getWeight();
                            if (b < a || a == -1) {
                                a = b;
                            }
                        } else {
                            int b = getLeastCost(map, all,
                                    item.getDestNodeId(), destPort);
                            int c = b + item.getWeight();
                            if (b != -1) {
                                if (a == -1) {
                                    a = c;
                                } else {
                                    if (c < a) {
                                        a = c;
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
        return a;
    }
}
